<template>
  <div class="car-buy-body">
    <div class="car-buy-item" v-loading="isloading">
      <el-row class="car-body">
        <el-col :span="8" class="left">
          <div class="swapper">
            <el-carousel
              height="150px"
              indicator-position="outside"
              arrow="never"
              v-if="dealswapperlist[0]"
              v-loading="isloading"
            >
              <el-carousel-item
                v-for="item in dealswapperlist"
                :key="item"
                class="img-item"
              >
                <img :src="item.url" alt="" />
              </el-carousel-item>
            </el-carousel>
          </div>
          <div class="info">
            <span class="deal">{{ carmessage.deal }}</span>
            <span class="price">{{ carmessage.price }}</span>
          </div>
        </el-col>
        <el-col :span="16" class="right">
          <el-row>
            <el-col :span="12" class="title">商品名称:</el-col>
            <el-col :span="12" class="carname">{{ carmessage.deal }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="12" class="title">商品单价:</el-col>
            <el-col :span="12" class="oneprice">{{ carmessage.price }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="12" class="title">商品总价:</el-col>
            <el-col :span="12" class="allprice">{{ allprice }}</el-col>
          </el-row>
          <el-divider />
          <h3 class="order-message">订单详情</h3>
          <el-row>
            <el-col :span="24" class="car-number">
              <span class="title">商品数量:</span
              ><input
                type="number"
                v-model="carnumber"
                oninput="if(value<2)value=1"
              />
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24" class="car-number">
              <span class="title">送货地址:</span
              ><input type="text" v-model="address" />
            </el-col>
          </el-row>
          <el-row class="button-item">
            <el-col :span="12">
              <el-button type="success" @click="pay">微 信 支 付</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { paymoney } from "untils/order.js";
import { getuserinfo, request } from "network/request.js";
import { ElMessage } from 'element-plus'
export default {
  data() {
    return {
      id: "",
      isloading: false,
      dealswapperlist: [],
      carmessage: {},
      carnumber: 1,
      price: 0,
      address: "",
      userinfo: {
        id: "",
        userid: "",
        usernumber: ''
      },
      total: 0,
      isget: false,
    };
  },
  computed: {
    //
    allprice() {
      if (this.isget == true) {
        let price = this.carmessage.price.split(" ");
        let trueprice = price[1].split(",")[0];
        let allcarprice = this.carnumber * parseInt(trueprice);
        return "$" + " " + allcarprice + ",00";
      }
      return 1;
    },
  },
  async created() {
    this.isloading = true;
    this.id = this.$route.query.id;
    let usertoken = localStorage.getItem("token");
    await getuserinfo(usertoken).then(({ data: res }) => {
      this.userinfo = JSON.parse(JSON.stringify(res[0]));
    });
    await request(this.id).then(({ data: res }) => {
      res[0].dealswapperlist.forEach((item) => {
        this.dealswapperlist.push(item);
      });
      this.carmessage = JSON.parse(JSON.stringify(res[0].carmessage));
      this.isloading = false;
      this.isget = true;
    });
  },
  methods: {
    pay() {
      if (this.address == "") {
        return ElMessage.warning('请选择收货地址!');
      }
      let type = '汽车购买'
      let price = this.carmessage.price.split(" ");
      let trueprice = price[1].split(",")[0];
      let goodlist = [
        {
          goodid: this.id,
          goodnum: this.carnumber
        }
      ]
      // let goodobj = {
      //     goodid: this.id,
      //     goodnum: this.carnumber
      //   }
      // for(let i=0;i< this.carnumber;i++) {
      //   goodlist.push(goodobj)
      // }
      this.total = this.carnumber * parseInt(trueprice);
      paymoney(goodlist,this.userinfo.userid, this.address, this.total,this.userinfo.usernumber,type).then(() => {
        ElMessage.success('购买成功~')
      })
      //   console.log(userorderinfo);
    },
  },
};
</script>

<style scoped lang="scss">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.car-buy-body {
  overflow: hidden;
  width: 100%;
  height: 100vh;
  background: linear-gradient(45deg, rgb(47, 188, 244), rgb(46, 153, 235));
}
.car-buy-item {
  margin: 9% auto;
  width: 60%;
  height: 60%;
  background-color: #fff;
  box-shadow: 3px 3px 2px #888;
  border-radius: 20px;
  .car-body {
    display: flex;
  }
  > .el-row {
    width: 100%;
    height: 100%;
    .left {
      border-radius: 20px 0 0px 20px;
      background: linear-gradient(45deg, rgb(245, 218, 163), rgb(245, 192, 85));
      position: relative;
      .swapper {
        width: 90%;
        margin: 40% auto 0;
        //   background-color: r#ed;
        /deep/ .is-active .el-carousel__button {
          background-color: #fff;
        }
        /deep/ .el-carousel__button {
          width: 10px;
          height: 10px;
          border: 1px solid #fff;
          border-radius: 50%;
        }
        .img-item img {
          width: 100%;
          height: 100%;
        }
      }
      .info {
        position: absolute;
        top: 8%;
        left: 4%;
        color: #fff;
        // background-color: #fff;
        > span {
          display: block;
          font-family: "Times New Roman", Times, serif;
        }
        .deal {
          letter-spacing: 2px;
          margin-bottom: 5px;
        }
        .price {
          font-size: 28px;
          font-weight: 700;
        }
      }
    }
    .right {
      padding: 10px 30px;
      background-color: #fff;
      border-radius: 0 20px 20px 0;
      input {
        border: none;
        border-bottom: 2px solid #eee;
        background-color: #fff;
        height: 30px;
        width: 70%;
        padding-left: 10px;
        outline: none;
        color: #666;
        font-weight: 600;
        font-size: 18px;
      }
      .button-item {
        margin-top: 20px;
      }
      .el-button {
        width: 100%;
        height: 40px;
      }
      .el-row {
        height: 40px;
        line-height: 40px;
      }
      .title {
        letter-spacing: 2px;
        // font-weight: 600;
        font-size: 14px;
        font-family: "黑体";
        color: #333;
      }
      .car-number {
        display: flex;
        span {
          display: block;
          width: 30%;
        }
      }
      .oneprice {
        font-weight: 600;
        color: orange;
      }
      .allprice {
        font-size: 24px;
        letter-spacing: 1px;
        color: green;
        font-weight: 700;
      }
      .carname {
        font-weight: 700;
        font-family: "Times New Roman", Times, serif;
      }
      .order-message {
        letter-spacing: 2px;
        height: 80px;
        line-height: 80px;
      }
    }
  }
}
</style>